/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package klient;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 *
 * @author Seba
 */
public class KlientView extends javax.swing.JPanel implements Runnable
{

    Socket gniazdko;

    public KlientView()
    {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfHost = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tfPort = new javax.swing.JTextField();
        btPolacz = new javax.swing.JButton();
        lbStatus = new javax.swing.JLabel();
        lbWiadomosc = new javax.swing.JLabel();
        btPotwierdz = new javax.swing.JButton();

        jLabel1.setText("Host:");

        tfHost.setText("127.0.0.1");

        jLabel2.setText("Port:");

        tfPort.setText("1234");

        btPolacz.setText("Połącz");
        btPolacz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPolaczActionPerformed(evt);
            }
        });

        lbWiadomosc.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        btPotwierdz.setText("Odpowiedz");
        btPotwierdz.setEnabled(false);
        btPotwierdz.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPotwierdzActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbWiadomosc)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfHost, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btPolacz)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbStatus))
                    .addComponent(btPotwierdz))
                .addGap(0, 190, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(tfPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btPolacz)
                    .addComponent(lbStatus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbWiadomosc)
                .addGap(18, 18, 18)
                .addComponent(btPotwierdz)
                .addContainerGap(28, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btPolaczActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPolaczActionPerformed
        try
        {
            Thread watek;
            if (tfHost.isEnabled())
            {
                gniazdko = new Socket(tfHost.getText(), Integer.parseInt(tfPort.getText()));
                gniazdko.setTcpNoDelay(true);

                watek = new Thread(this);
                watek.start();

                btPolacz.setText("Rozłącz");
                tfHost.setEnabled(false);
                tfPort.setEnabled(false);
                btPotwierdz.setEnabled(true);

                lbStatus.setText("Połączono");
            } else
            {
                gniazdko.close();

                btPotwierdz.setEnabled(false);
                btPolacz.setText("Połącz");
                tfHost.setEnabled(true);
                tfPort.setEnabled(true);
                lbStatus.setText("Rozłączono");
                lbWiadomosc.setText("");
            }
        } catch (Exception e)
        {
            lbStatus.setText("Brak połączenia");
        }        // TODO add your handling code here:
    }//GEN-LAST:event_btPolaczActionPerformed

    private void btPotwierdzActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPotwierdzActionPerformed
        try
        {
            BufferedWriter wyslij = new BufferedWriter(new OutputStreamWriter(gniazdko.getOutputStream()));
            wyslij.write("odebrano\r\n");
            wyslij.flush();
        } catch (Exception e)
        {
        }
    }//GEN-LAST:event_btPotwierdzActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btPolacz;
    private javax.swing.JButton btPotwierdz;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lbStatus;
    private javax.swing.JLabel lbWiadomosc;
    private javax.swing.JTextField tfHost;
    private javax.swing.JTextField tfPort;
    // End of variables declaration//GEN-END:variables

    public void run()
    {
        int licznik = 0;
        try
        {
            BufferedReader odbieranie;
            String wiadomoscTresc = new String();
            for (;;)
            {
                odbieranie = new BufferedReader(new InputStreamReader(gniazdko.getInputStream()));
                String odebrane = odbieranie.readLine();
                System.out.println(odebrane);
                
                String[] wiadomosc = odebrane.split(" ");        
                
                if (!wiadomosc[0].equals("ok"))
                    wiadomoscTresc = odebrane.substring(wiadomosc[0].length() + 1);

                if (wiadomosc[0].equals("ponowienie"))
                {
                    licznik++;
                    lbWiadomosc.setText("Ponowiono " + String.valueOf(licznik) + ": " + wiadomoscTresc);
                }

                if (wiadomosc[0].equals("wiadomosc"))
                {
                    licznik = 0;
                    lbWiadomosc.setText("Wiadomosc: " + wiadomoscTresc);
                }

                if (wiadomosc[0].equals("ok"))
                {
                    licznik = 0;
                    lbWiadomosc.setText("");
                }

            }
        } catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}
